//
//  PackageDetailViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/24.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZLayout.h"
#import "GKPhotoBrowser.h"

#import <ReactiveObjC.h>
#import <BRPickerView.h>
#import "XLPageViewController.h"

#import "PackageDetailRequest.h"

#import "PackageDetailNaviView.h"
#import "PackageDetailBottomView.h"

#import "PackageDetailModule.h"

#import "IMBottomBubbleView.h"

#import "PackageDetailBannerCell.h"
#import "PackageDetailExplainCell.h"
#import "PackageDetailInfoCell.h"
#import "PackageDetailCouponCell.h"
#import "PackageDetailInfoShowCell.h"
#import "PackageDetailStoreInfoCell.h"
#import "PackageDetailRecommandCell.h"
#import "PackageDetailStepCell.h"

#import "ProductDetailNormalSectionView.h"
#import "ProductDetailEvaluationHeader.h"
#import "PackageDetailSectionHeader.h"

#import "ProductDetailSectionFooterView.h"

#import "TT_BaseCollectionView.h"

#import "ImageTextTableViewController.h"
#import "ImageTextTableViewController.h"


#import "RelatedCasesViewController.h"
#import "ServiceContentViewController.h"
#import "SubmitSerOrderViewController.h"
#import "StoreDetailViewController.h"

#import "EvaluationDetailViewController.h"
#import "TT_ConversationDetailViewController.h"
#import "MessageCenterViewController.h"
#import "EvaluateListViewController.h"
#import "PromisePopViewController.h"
#import "TT_WebviewController.h"
#import "PackageDetailViewController.h"
//快问
#import "QuanYiViewController.h"

#import "WYXViewController.h"

#import "TIIMQuickReplyCardModel.h"
#import "StoreQuickReplyView.h"

@interface PackageDetailViewController ()
<UICollectionViewDelegate,
UICollectionViewDataSource,
GKPhotoBrowserDelegate,
ZZLayoutDelegate,
XLPageViewControllerDelegate,
XLPageViewControllerDataSrouce>

@property (nonatomic, strong) PackageDetailNaviView *naviView;
@property (nonatomic, strong) TT_BaseCollectionView *collectionView;
@property (nonatomic, strong) PackageDetailBannerCell *mediaCell;
@property (nonatomic, strong) PackageDetailBottomView *bottomView;
@property (nonatomic, strong) IMBottomBubbleView *bubbleView;

@property (nonatomic ,assign) BOOL canScroll;
@property (nonatomic ,assign) BOOL isQuanYiShow;

@property (nonatomic, assign) CGFloat recommandHeight;
@property (nonatomic, strong) UICollectionViewCell *cell;
@property (nonatomic, strong) NSMutableArray *segArray;
@property (nonatomic, strong) NSMutableArray *segTitles;
@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) ImageTextTableViewController    *imgAndTextVC;
@property (nonatomic, strong) RelatedCasesViewController    *relatedCaseVC;
@property (nonatomic, strong) ServiceContentViewController  *serviceContentVC;

@property (nonatomic, strong) QuanYiViewController *quanYiVC;

@property (nonatomic, strong) PackageDetailModule *packageDetailmodel;

@property (nonatomic, assign) CGFloat collectionHeight;
@property (nonatomic, assign) CGFloat recommandVCHeight;

@property (nonatomic, assign) NSTimeInterval lastTime;

//当前 显示状态   权益or collectionview
//快问
@property(nonatomic,strong)TIIMQuickReplyCardModel *quickReplyModel;
@property(nonatomic,strong)BaseAdjustButton *quickReplyButton;
@property(nonatomic,weak)StoreQuickReplyView *weakQuickReplyView;
@end

@implementation PackageDetailViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self request_packageDetail];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessAction) name:@"BA_LoginSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showQuanyi) name:@"packageShowQuanYiNoti" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideQuanyi) name:@"packageHideQuanYiNoti" object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleStep) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleStep) name:UIApplicationWillTerminateNotification object:nil];
    
    self.lastTime = [Utilities getCurrentServerTimestamp];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];

    [self.mediaCell pause];

    [self handleStep];
}

- (void)appBecomeActive {
    self.lastTime = [Utilities getCurrentServerTimestamp];
}

- (void)handleStep {

    [[BA_BusinessHelper shared] addFootPrintWith:self.packageId lastTime:self.lastTime And:self.packageDetailmodel.sellerModel.sellerId type:BusinessCollectType_package];
}

- (void)loginSuccessAction {
    [self.collectionView reloadData];
}

- (void)initView {
    
    WEAKSELF;
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:@"tabNoti" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        weakSelf.canScroll = YES;
        [weakSelf subTabViewCanScroll:NO];
    }];
    self.canScroll = YES;
    self.isQuanYiShow = NO;
    self.collectionHeight = TTScreenHeight - Bottom_SafeHeight - 60;
    self.recommandVCHeight = TTScreenHeight -kStatusBarHeight - NavBar_Height - Bottom_SafeHeight - 60;

    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    self.view.backgroundColor = UIColorHex(0xf1f1f1);
    
    [RACObserve([TT_MessageManager manager], totalUnReadCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].totalUnReadCount > 0) {
            weakSelf.naviView.msgBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].totalUnReadCount];
            weakSelf.naviView.msgBtn.badgeBGColor = [UIColor redColor];
        } else {
            weakSelf.naviView.msgBtn.badgeValue = @"";
            weakSelf.naviView.msgBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
    [self.naviView refreshNaviFavorStatusWith:self.packageDetailmodel.isCollect];

    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.quanYiVC.view];

    [self.view addSubview:self.naviView];
    [self.view addSubview:self.bottomView];
    
    if (self.packageDetailmodel.sellerModel.consultingStar.length > 0) {

        CGFloat width = [Utilities getStringWidthWithString:self.packageDetailmodel.sellerModel.consultingStar fontSize:12 andHeight:36] + 70;
        self.bubbleView = [[IMBottomBubbleView alloc] initWithFrame:CGRectZero];
        [self.bubbleView.bubble setTitle:self.packageDetailmodel.sellerModel.consultingStar forState:UIControlStateNormal];
        
        [self.view addSubview:self.bubbleView];
        [self.bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(width);
            make.bottom.mas_equalTo(weakSelf.bottomView.mas_top);
        }];
        
        [self.view layoutIfNeeded];
    } else {
        self.bubbleView.hidden = YES;
    }
    [self.bottomView layoutIfNeeded];
    [self.collectionView layoutIfNeeded];
    [self.view layoutIfNeeded];

    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(kStatusBarHeight + NavBar_Height);
    }];
        
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(weakSelf.collectionHeight);
    }];
    
    [self.quanYiVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(weakSelf.collectionView.mas_bottom);
        make.height.mas_equalTo(weakSelf.recommandVCHeight);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(60 + Bottom_SafeHeight);
    }];
    
    //这些计算需要在网络请求后和reloaddata前刷新
    self.recommandHeight = TTScreenHeight - NavBar_Height - kStatusBarHeight - 60 - Bottom_SafeHeight;
    [self addChildViewController:self.pageViewController];

    [self initViewAction];
    
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
    if (self.packageDetailmodel.bannerPics.count > 0) {
        [self showImageWithArray:self.packageDetailmodel.bannerPics index:index];
    }
}

- (void)showImageWithArray:(NSArray *)imageUrls index:(NSInteger)index {
    NSMutableArray *photos = [NSMutableArray new];
    
    [imageUrls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:obj];
        [photos addObject:photo];
    }];
    
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:index];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;
    
    browser.delegate  = self;
    [browser showFromVC:self];
}

- (void)goBuy {
    
    if ([TTUserModel sharedObject].token.length > 0) {
        SubmitSerOrderViewController *vc = [[SubmitSerOrderViewController alloc] init];
        vc.model = self.packageDetailmodel;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {}];
    }
}

- (void)initViewAction {
    
    WEAKSELF
    self.bubbleView.IMBottomBubbleViewBlock = ^(BubbleActionType type) {
        switch (type) {
            case BubbleActionType_close:{
                [weakSelf.bubbleView removeFromSuperview];
            } break;
            case BubbleActionType_jump:{
                [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.packageDetailmodel.sellerModel.sellerId];
            } break;
            default:
                break;
        }
    };
    
    self.naviView.PackageDetailNaviViewBlock = ^(PackageDetailNaviViewActionType actionType) {
        
        switch (actionType) {
            case PackageDetailNaviViewActionType_back:{
                [weakSelf goBack];
            } break;
            case PackageDetailNaviViewActionType_collect:{

                [[BA_BusinessHelper shared] callCollectStoreWith:weakSelf.packageId
                                                   currentStatus:weakSelf.packageDetailmodel.isCollect
                                                            type:BusinessCollectType_package
                                                        comption:^(BOOL success) {
                    weakSelf.packageDetailmodel.isCollect = !weakSelf.packageDetailmodel.isCollect;
                    [weakSelf.naviView refreshNaviFavorStatusWith:weakSelf.packageDetailmodel.isCollect];
                }];

            } break;
            case PackageDetailNaviViewActionType_im:{
                MessageCenterViewController *vc = [[MessageCenterViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
            case PackageDetailNaviViewActionType_share:{
                [weakSelf openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                    if (isLogin) {
                        [weakSelf share];
                    }
                }];
            } break;
            default:
                break;
        }
    };
    
    self.naviView.PackageDetailSegBlock = ^(SegmentPackageDetailSelType type) {

        weakSelf.canScroll = YES;
        [weakSelf subTabViewCanScroll:NO];

        if (type != SegmentPackageDetailSelType_Quanyi) {
            
            [weakSelf hideQuanyi];
            [weakSelf scrollCollectionWithType:type];
        } else {

            [weakSelf showQuanyi];
        }
    };
    
    
    self.bottomView.PackageDetailBottomViewBlock = ^(PackageDetailBottomActionType actionType) {
      
        switch (actionType) {
            case PackageDetailBottomActionType_im:{

                [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.packageDetailmodel.sellerModel.sellerId];

            } break;
            case PackageDetailBottomActionType_phone:{
                [[TT_PhonaCallManager manager] callWithStoreId:weakSelf.packageDetailmodel.sellerModel.sellerId];

            } break;
            case PackageDetailBottomActionType_yuyue:{
                
                [[BA_BusinessHelper shared] callSubscribeStoreWith:weakSelf.packageDetailmodel.sellerModel.sellerId];
                
            } break;
            case PackageDetailBottomActionType_buy:{
                
                [weakSelf goBuy];
            } break;
            default:
                break;
        }
    };
}



- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark Share
- (void)share {
    CommonShareModel *model = [[CommonShareModel alloc] init];
    model.url = [NSString stringWithFormat:@"%@%@?goodsId=%@&myCode=%@",BA_GlobalH5_Domain,@"/setMealShare",self.packageId,[TTUserModel sharedObject].myCode];
    model.title = self.packageDetailmodel.sellerGoodsModel.goodsName;
    model.content = @"婚前婚后邀您体验";
    model.image = self.packageDetailmodel.bannerPics.firstObject;
    [[TT_ShareManager share] callShareViewWithModel:model];
}

#pragma mark- 协议方法
//collectionView的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 7;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    switch (section) {
        case PackageDetailSectionType_PackageInfo:{
            return 8;
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
            return 0;
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
            return 0;
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{
            return 1;
        } break;
        case PackageDetailSectionType_StoreRecommend:{
            
            if (self.packageDetailmodel.sellerGoodsList.count>4) {
                return 4;
            } else {
                return self.packageDetailmodel.sellerGoodsList.count;
            }
            
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
            return 1;
        } break;
        default:
            break;
    }
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
        
    WEAKSELF
    if (kind == UICollectionElementKindSectionHeader) {
        
        switch (indexPath.section) {
            case PackageDetailSectionType_PackageInfo:{
            } break;
            case PackageDetailSectionType_PackageEvaluationTag:{

                ProductDetailNormalSectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailNormalSectionView" forIndexPath:indexPath];
                headerView.sectionTitle.text = @"用户评价";
                headerView.sectionSubTitle.text = @"查看全部";
                
                [headerView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
                    
                    EvaluateListViewController *vc = [[EvaluateListViewController alloc] init];

                    if ([weakSelf.packageDetailmodel.sellerModel.classId isEqualToString:Store_Category_Car]) {
                        vc.storeId = weakSelf.packageDetailmodel.sellerModel.sellerId;
                    } else {
                        vc.goodsId = weakSelf.packageId;
                    }
                    [weakSelf.navigationController pushViewController:vc animated:YES];

                }];
                
                return headerView;
                
            } break;
            case PackageDetailSectionType_PackageEvaluation:{

                ProductDetailEvaluationHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailEvaluationHeader" forIndexPath:indexPath];
                
                if (self.packageDetailmodel.evaluateArray.count > 0) {
                    [headerView fillHeadWithModel:self.packageDetailmodel.evaluateArray[0]];
                }
                
                WEAKSELF
                [headerView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
                   
                    EvaluationDetailViewController *vc = [[EvaluationDetailViewController alloc] init];
                    vc.ueId = weakSelf.packageDetailmodel.evaluateArray.firstObject.ueId;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
                
                return headerView;
                
            } break;
            case PackageDetailSectionType_PackageStoreInfo:{
            } break;
            case PackageDetailSectionType_StoreRecommend:{
                
                ProductDetailNormalSectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailNormalSectionView" forIndexPath:indexPath];
                headerView.sectionTitle.text = @"店铺推荐";
                headerView.sectionSubTitle.text = @"";
                return headerView;
                
            } break;
            case PackageDetailSectionType_PackageGraphicDetail:{
                
                PackageDetailSectionHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PackageDetailSectionHeader" forIndexPath:indexPath];
                headerView.sectionTitleLab.text = @"套餐详情";

                return headerView;
                
            } break;
            default:
                break;
        }

        ProductDetailNormalSectionView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailNormalSectionView" forIndexPath:indexPath];

        return footerView;
        
    } else {
        ProductDetailSectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ProductDetailSectionFooterView" forIndexPath:indexPath];

        return footerView;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case PackageDetailSectionType_PackageInfo:{
            switch (indexPath.row) {
                    
                case PackageInfoCellType_explain:{
                    
                    PromisePopViewController *pop = [[PromisePopViewController alloc]initDefaultHeight:0.6*TTScreenHeight vc:self dataSource:@[].mutableCopy];
                    STPopupController *popVericodeController = [[STPopupController alloc] initWithRootViewController:pop];
                    popVericodeController.style = STPopupStyleBottomSheet;
                    [popVericodeController presentInViewController:self];
                    
                } break;
                case PackageInfoCellType_info:{
                } break;
                case PackageInfoCellType_orderGift:{
                    [self goBuy];
                } break;
                case PackageInfoCellType_allPayGift:{
                    [self goBuy];
                } break;
                default:
                    break;
            }
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{

            StoreDetailViewController *vc = [[StoreDetailViewController alloc] init];
            vc.storeId = self.packageDetailmodel.sellerModel.sellerId;
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case PackageDetailSectionType_StoreRecommend:{

            CommonPackageModel *model = self.packageDetailmodel.sellerGoodsList[indexPath.row];
            [[TT_JumpManager sharedManager] jumpWithDetailModel:model completion:^(BOOL success, NSString * _Nonnull message) {}];

            
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
        } break;
        default:
            break;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        
    WEAKSELF
    switch (indexPath.section) {
        case PackageDetailSectionType_PackageInfo:{

            switch (indexPath.row) {
                    
                case PackageInfoCellType_banner: {
                    self.mediaCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailBannerCellID" forIndexPath:indexPath];
                                        
                    NSString *maskUrl = @"";
                    if (self.packageDetailmodel.videofengmianList.count > 0) {
                        maskUrl =self.packageDetailmodel.videofengmianList.firstObject.url;
                    }
                    [self.mediaCell fillWithImgArray:self.packageDetailmodel.bannerPics videoUrl:self.packageDetailmodel.videoUrl andMaskUrl:maskUrl];
                    
                    self.mediaCell.bannerIndexClicked = ^(NSInteger index) {
                        
                        if (weakSelf.packageDetailmodel.bannerPics.count > 0) {
                            [weakSelf showImageWithArray:weakSelf.packageDetailmodel.bannerPics index:index];
                        }
                    };
                    
                    return self.mediaCell;
                } break;
                case PackageInfoCellType_explain:{

                    PackageDetailExplainCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailExplainCellID" forIndexPath:indexPath];
                    
                    return cell;
                    
                } break;
                case PackageInfoCellType_info:{
                    
                    PackageDetailInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailInfoCellID" forIndexPath:indexPath];
                    [cell fillCellWithModel:self.packageDetailmodel.sellerGoodsModel];
                    
                    WEAKSELF
                    cell.PackageDetailInfoBlock = ^{
                        
                        [weakSelf openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                            if (isLogin) {
                                [weakSelf.collectionView reloadData];
                            }
                        }];
                    };
                    
                    return cell;
                    
                } break;
                case PackageInfoCellType_orderGift:{

                    PackageDetailInfoShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailInfoShowCellID" forIndexPath:indexPath];
                    cell.titleLab.text = @"订单礼";
                    cell.subTitleLab.text = self.packageDetailmodel.sellerGoodsModel.goodsDepositGift;
                    cell.cacheLab.text = @"去下单";
                    return cell;
                    
                } break;
                case PackageInfoCellType_allPayGift:{

                    PackageDetailInfoShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailInfoShowCellID" forIndexPath:indexPath];
                    cell.titleLab.text = @"全款礼";
                    cell.subTitleLab.text = self.packageDetailmodel.sellerGoodsModel.goodsTotalGift;
                    cell.cacheLab.text = @"去下单";
                    return cell;
                    
                } break;
                case PackageInfoCellType_orderStep:{

                    PackageDetailStepCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailStepCellID" forIndexPath:indexPath];
                    return cell;
                    
                } break;
                default:
                    break;
            }
            
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
            
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
            
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{

            PackageDetailStoreInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailStoreInfoCellID" forIndexPath:indexPath];
            [cell fillCellWithModel:self.packageDetailmodel];
            
            cell.PackageDetailStoreInfoCellBlock = ^{
              
                WYXViewController *vc = [[WYXViewController alloc] init];
                vc.storeId = weakSelf.packageDetailmodel.sellerModel.sellerId;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            };
            return cell;
            
        } break;
        case PackageDetailSectionType_StoreRecommend:{

            PackageDetailRecommandCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailRecommandCellID" forIndexPath:indexPath];
            
            if (self.packageDetailmodel.sellerGoodsList.count > indexPath.row) {
                [cell fillCellWithModel:self.packageDetailmodel.sellerGoodsList[indexPath.row]];
            }
            
            return cell;
            
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{

//            return self.cell;
            
            self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cid" forIndexPath:indexPath];
            [self.cell addSubview:self.pageViewController.view];
            [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.bottom.mas_equalTo(0);
            }];
            return self.cell;
            
        } break;
        default:
            break;
    }

    PackageDetailExplainCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailExplainCellID" forIndexPath:indexPath];
    return cell;
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的高
    
//    WEAKSELF
    switch (indexPath.section) {
        case PackageDetailSectionType_PackageInfo:{

            switch (indexPath.row) {
                    
                case PackageInfoCellType_banner: {
                    return [PackageDetailBannerCell calculateHeight];;
                } break;
                case PackageInfoCellType_info:{
                    return  [PackageDetailInfoCell calculateHeight];
                } break;
                case PackageInfoCellType_explain:{
                    return  [PackageDetailExplainCell calculateHeight];
                } break;
                case PackageInfoCellType_orderGift:{
                    
                    if (self.packageDetailmodel.sellerGoodsModel.goodsDepositGift.length > 0) {
                        return [PackageDetailInfoShowCell calculateHeight];
                    }

                } break;
                case PackageInfoCellType_allPayGift:{
                    
                    if (self.packageDetailmodel.sellerGoodsModel.goodsTotalGift.length > 0) {
                        return [PackageDetailInfoShowCell calculateHeight];
                    }
                } break;
                case PackageInfoCellType_orderStep:{
                    return [PackageDetailStepCell calculateHeight];
                    
                } break;
                default:
                    break;
            }
            
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
            return CGFLOAT_MIN;
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
            return CGFLOAT_MIN;
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{
            
            CGFloat height = [PackageDetailStoreInfoCell calculateHeightWithModel:self.packageDetailmodel];
            return height;
        } break;
        case PackageDetailSectionType_StoreRecommend:{
            return [PackageDetailRecommandCell calculateHeight];
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
            return self.recommandHeight;
        } break;
        default:
            break;
    }

    return CGFLOAT_MIN;
}

- (NSInteger)layout:(ZZLayout *)layout columnNumberAtSection:(NSInteger)section {//每个区有几列
    
    switch (section) {
        case PackageDetailSectionType_PackageInfo:{
            return 1;
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
            return 1;
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
            return 1;
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{
            return 1;
        } break;
        case PackageDetailSectionType_StoreRecommend:{
            return 2;
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
            return 1;
        } break;
        default:
            break;
    }
    return 0;
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    
    switch (section) {
        case PackageDetailSectionType_PackageInfo:{
            return UIEdgeInsetsMake(0, 0, 0, 0);
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
            return UIEdgeInsetsMake(0, 0, 0, 0);
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
            return UIEdgeInsetsMake(0, 0, 0, 0);
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{
            return UIEdgeInsetsMake(0, 0, 0, 0);
        } break;
        case PackageDetailSectionType_StoreRecommend:{
            if (self.packageDetailmodel.sellerGoodsList.count > 0) {
                return UIEdgeInsetsMake(15, 15, 15, 15);
            } else {
                return UIEdgeInsetsMake(0, 0, 0, 0);
            }
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
            return UIEdgeInsetsMake(0, 0, 0, 0);
        } break;
        default:
            break;
    }
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距

    switch (section) {
        case PackageDetailSectionType_PackageInfo:{
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{
        } break;
        case PackageDetailSectionType_StoreRecommend:{
            if (self.packageDetailmodel.sellerGoodsList.count > 0) {
                return 10;
            }
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
        } break;
        default:
            break;
    }
    
    return 0;
}

- (CGFloat)layout:(ZZLayout*)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距

    switch (section) {
        case PackageDetailSectionType_PackageInfo:{
            return 1;
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{
        } break;
        case PackageDetailSectionType_PackageEvaluation:{
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{
        } break;
        case PackageDetailSectionType_StoreRecommend:{
            
            if (self.packageDetailmodel.sellerGoodsList.count > 0) {
                return 10;
            }
            
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
        } break;
        default:
            break;
    }
    return CGFLOAT_MIN;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {//设置区头的高度
    
    switch (section) {
        case PackageDetailSectionType_PackageInfo:{
            
        } break;
        case PackageDetailSectionType_PackageEvaluationTag:{

            if (self.packageDetailmodel.evaluateArray.count > 0) {
                return CGSizeMake(TTScreenWidth, [ProductDetailNormalSectionView calculateHeight]);
            } else {
                return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
            }

        } break;
        case PackageDetailSectionType_PackageEvaluation:{

            if (self.packageDetailmodel.evaluateArray.count > 0) {
                return CGSizeMake(TTScreenWidth, self.packageDetailmodel.evaluateArray.firstObject.itemHeight);
            } else {
                return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
            }
            
        } break;
        case PackageDetailSectionType_PackageStoreInfo:{

        } break;
        case PackageDetailSectionType_StoreRecommend:{

            if (self.packageDetailmodel.sellerGoodsList.count > 0) {
                return CGSizeMake(TTScreenWidth, [ProductDetailNormalSectionView calculateHeight]);
            } else {
                return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
            }
        } break;
        case PackageDetailSectionType_PackageGraphicDetail:{
            return CGSizeMake(TTScreenWidth, [PackageDetailSectionHeader calculateHeight]);

        } break;
        default:
            break;
    }
    
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForFooterInSection:(NSInteger)section {//设置区尾的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGFloat)layout:(ZZLayout*)layout spacingWithLastSectionForSectionAtIndex:(NSInteger)section {//本区区头和上个区区尾的间距
    return 0;
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {//设置不同分区的不同背景颜色
    return UIColorHex(0xffffff);
}

#pragma mark 悬停的关键代码

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
            
    if (_quickReplyButton) {
        WK(weakSelf)
        CGFloat right = WIDTH + (weakSelf.quickReplyButton.width - 24*ADAPTER_WIDTH);
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.quickReplyButton.right = right;
        }];
    }
    if (scrollView.tag !=10086) {
        return;
    }
    
    CGFloat naviH = self.packageDetailmodel.detail_y;

    if (scrollView.contentOffset.y>naviH) {
        scrollView.contentOffset = CGPointMake(0, naviH);
        if (self.canScroll) {
            self.canScroll = NO;
            [self subTabViewCanScroll:YES];
        }
    }else{
        if (!self.canScroll) {
            scrollView.contentOffset = CGPointMake(0, naviH);
        }
    }
    
    //    导航栏设置
    CGFloat height = (300 - kStatusBarHeight - NavBar_Height);
    
    CGFloat alpha = scrollView.contentOffset.y/height;
    if (scrollView.contentOffset.y > 0.9 * height) {
        [self.naviView fillWithNaviStatus:GoodsNaviViewStatusType_white isCollect:self.packageDetailmodel.isCollect alpha:alpha];
    } else {
        [self.naviView fillWithNaviStatus:GoodsNaviViewStatusType_clear isCollect:self.packageDetailmodel.isCollect alpha:alpha];
    }
    
    [self refreshNaviWith:scrollView.contentOffset.y];
}

- (void)refreshNaviWith:(CGFloat)offset_y {
    if (offset_y >=self.packageDetailmodel.package_y && offset_y < self.packageDetailmodel.eva_y-1) {
        [self.naviView fillWithSegType:SegmentPackageDetailSelType_package];
    } else if (offset_y >=self.packageDetailmodel.eva_y-1 && offset_y < self.packageDetailmodel.detail_y-1) {
        [self.naviView fillWithSegType:SegmentPackageDetailSelType_evaluate];
    } else if (offset_y >=self.packageDetailmodel.detail_y-1){
        if (!self.isQuanYiShow) {
            [self.naviView fillWithSegType:SegmentPackageDetailSelType_Detail];
        }
    }
}

- (void)scrollCollectionWithType:(SegmentPackageDetailSelType)type {
    CGFloat y=0;
    switch (type) {
        case SegmentPackageDetailSelType_package:{
            y = self.packageDetailmodel.package_y;
        } break;
        case SegmentPackageDetailSelType_evaluate:{
            y = self.packageDetailmodel.eva_y;
        } break;
        case SegmentPackageDetailSelType_Detail:{
            y = self.packageDetailmodel.detail_y;
        } break;
        case SegmentPackageDetailSelType_Quanyi:{
            y = self.packageDetailmodel.quanyi_y;
        } break;

        default:
            break;
    }
    
    NSLog(@"scrollCollectionWithType = %lf",y);
    [self.collectionView setContentOffset:CGPointMake(0, y) animated:YES];
    self.collectionView.bouncesZoom = NO;
}

- (void)showQuanyi {
    WEAKSELF
    self.isQuanYiShow = YES;
    [self.naviView fillWithSegType:SegmentPackageDetailSelType_Quanyi];
    [UIView animateWithDuration:0.2 animations:^{
        [weakSelf.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(weakSelf.collectionHeight);
            make.top.mas_equalTo(-weakSelf.recommandVCHeight);
        }];
        [weakSelf.quanYiVC.view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(weakSelf.collectionView.mas_bottom);
            make.height.mas_equalTo(weakSelf.recommandVCHeight);
        }];
        [weakSelf.collectionView.superview layoutIfNeeded];
        [weakSelf.quanYiVC.view.superview layoutIfNeeded];
    }];
}

- (void)hideQuanyi {
    self.isQuanYiShow = NO;
    WEAKSELF
    [self refreshNaviWith:self.collectionView.contentOffset.y];
    [UIView animateWithDuration:0.2 animations:^{
        [weakSelf.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(weakSelf.collectionHeight);
            make.top.mas_equalTo(0);
        }];
        [weakSelf.quanYiVC.view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(weakSelf.collectionView.mas_bottom);
            make.height.mas_equalTo(weakSelf.recommandVCHeight);
        }];
        [weakSelf.collectionView.superview layoutIfNeeded];
        [weakSelf.quanYiVC.view.superview layoutIfNeeded];
    }];
}

-(void)subTabViewCanScroll:(BOOL)canScroll{
    
    for (UIViewController *subVC in self.segArray) {
        
        if ([subVC isKindOfClass:[ImageTextTableViewController class]]) {
            
            self.imgAndTextVC.canScroll = canScroll;
            if (!canScroll) {
//                self.imgAndTextVC.tableview.contentOffset = CGPointZero;
            }
        }
        
        if ([subVC isKindOfClass:[ServiceContentViewController class]]) {
            
            self.serviceContentVC.canScroll = canScroll;
            if (!canScroll) {
//                self.serviceContentVC.tableview.contentOffset = CGPointZero;
            }
        }
        
        if ([subVC isKindOfClass:[RelatedCasesViewController class]]) {
            
            self.relatedCaseVC.canScroll = canScroll;
            if (!canScroll) {
//                self.relatedCaseVC.tableview.contentOffset = CGPointZero;
            }
        }
    }
}

#pragma mark - XLPageViewController
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
        
    return self.segArray[index];
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.segTitles[index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.segArray.count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    NSLog(@"切换%ld",index);
}

- (XLPageViewController *)pageViewController {
    
    if (!_pageViewController) {
        XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
        config.shadowLineColor = UIColorHex(0xff4242);
        config.titleSelectedColor = UIColorHex(0xff4242);
        config.titleNormalColor = UIColorHex(0x666666);
        config.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
        config.titleNormalFont = [UIFont systemFontOfSize:14];
        config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
        config.separatorLineHidden = true;
        config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
        config.shadowLineWidth = 20;
        config.titleSpace = 0;
        config.titleWidth = 100;
        config.titleViewHeight = 50;

        _pageViewController = [[XLPageViewController alloc] initWithConfig:config];
        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;
        
        [_pageViewController registerNib:[UINib nibWithNibName:@"HomeSegTitleCell" bundle:nil] forTitleViewCellWithReuseIdentifier:@"HomeSegTitleCellID"];
        
            for (NSString *typeTitle in self.segTitles) {
            
                if ([typeTitle isEqualToString:@"图文介绍"]) {
                    self.imgAndTextVC.model = self.packageDetailmodel;
                    [self.segArray addObject:self.imgAndTextVC];
                }
        
                if ([typeTitle isEqualToString:@"服务内容"]) {
                    if (self.packageDetailmodel.sellerGoodsServiceFlag.sgsServiceParameter.length == 0) {
                    } else {
                        [self.segArray addObject:self.serviceContentVC];
                    }
                }
        
                if ([typeTitle isEqualToString:@"相关案例"]) {
                    if (self.packageDetailmodel.sellerOpusList.count == 0) {
                    } else {
                        [self.segArray addObject:self.relatedCaseVC];
                    }
                }
            }
    }
    return _pageViewController;
}

- (NSMutableArray *)segTitles {
    if (!_segTitles) {
        _segTitles = @[@"图文介绍",@"服务内容",@"相关案例"].mutableCopy;
    }
    return _segTitles;
}

-(NSMutableArray *)segArray{

    if (!_segArray) {
        _segArray = [NSMutableArray array];
    }
    return _segArray;
}

- (ImageTextTableViewController *)imgAndTextVC {
    if (!_imgAndTextVC) {
        _imgAndTextVC = [[ImageTextTableViewController alloc] init];
    }
    return _imgAndTextVC;
}

- (RelatedCasesViewController *)relatedCaseVC {
    if (!_relatedCaseVC) {
        _relatedCaseVC = [[RelatedCasesViewController alloc] init];
        _relatedCaseVC.model = self.packageDetailmodel;

    }
    return _relatedCaseVC;
}

- (ServiceContentViewController *)serviceContentVC {
    if (!_serviceContentVC) {
        _serviceContentVC = [[ServiceContentViewController alloc] init];
        _serviceContentVC.model = self.packageDetailmodel;
    }
    return _serviceContentVC;
}

- (QuanYiViewController *)quanYiVC {
    if (!_quanYiVC) {
        _quanYiVC = [[QuanYiViewController alloc] init];
    }
    return _quanYiVC;
}

- (PackageDetailNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"PackageDetailNaviView"owner:nil options:nil].firstObject;
    }
    return _naviView;
}

- (PackageDetailBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[NSBundle mainBundle] loadNibNamed:@"PackageDetailBottomView"owner:nil options:nil].firstObject;
    }
    return _bottomView;
}

- (PackageDetailModule *)packageDetailmodel {
    if (!_packageDetailmodel) {
        _packageDetailmodel = [[PackageDetailModule alloc] init];
    }
    return _packageDetailmodel;
}

#pragma mark- 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeVertical delegate:self];
        //打开区头悬浮功能(默认关闭)
        layout.sectionHeadersPinToVisibleBounds = NO;
        
        _collectionView = [[TT_BaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = UIColorHex(0xf1f1f1);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.tag = 10086;
        
        [_collectionView registerClass:[PackageDetailBannerCell class] forCellWithReuseIdentifier:@"PackageDetailBannerCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailExplainCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailExplainCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailInfoCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailInfoCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailCouponCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailCouponCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailInfoShowCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailInfoShowCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailStoreInfoCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailStoreInfoCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailRecommandCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailRecommandCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailStepCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailStepCellID"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailExplainCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailExplainCellID"];

        
        [_collectionView registerNib:[UINib nibWithNibName:@"ProductDetailNormalSectionView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailNormalSectionView"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailSectionHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PackageDetailSectionHeader"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"ProductDetailEvaluationHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailEvaluationHeader"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"ProductDetailSectionFooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ProductDetailSectionFooterView"];
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cid"];
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;

        _collectionView.bounces = YES;
        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}

- (void)request_packageDetail {

    WEAKSELF
    [PackageDetailRequest request_packageDetailWith:self.packageId statistics:self.shouldStatistics comption:^(BOOL success, PackageDetailModule * _Nonnull model) {
            
        if (model.sellerGoodsModel.goodsStatus == 2) {
            [TT_Alert alert:@"该套餐已下架" doneAction:^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }];
            return;
        }
        
        if (success) {
            weakSelf.packageDetailmodel = model;
            [weakSelf initView];
            [weakSelf.collectionView reloadData];
            
            if ([TTUserModel sharedObject].userId.length > 0) {
                [weakSelf addQuickReply];
            }
            
            if ([TTUserModel sharedObject].userId.length > 0) {
                [[BA_BusinessHelper shared] browseStore:model.sellerModel.sellerId
                                                   type:BusinessActionType_tcs
                                               comption:^(BOOL success) {}];
            }

        }
    }];
}

#pragma mark - 快捷回复模块
- (void)addQuickReply
{
    dispatch_async(dispatch_get_main_queue(), ^{
        //获取快捷回复
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setValue:[NSString stringWithFormat:@"%@",self.packageDetailmodel.sellerModel.sellerId] forKey:@"sellerId"];
        WK(weakSelf)
        [TTHttpManager postRequestWithUrl:[NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/userProblem/userSelectList"] TimeOut:10 PostData:parameters FinishBlock:^(id  _Nonnull responseObject) {
            TIIMQuickReplyCardModel *responModel = [TIIMQuickReplyCardModel modelWithJSON:responseObject];
            if (responModel.success) {
                if (responModel.data.count > 0) {
                    weakSelf.quickReplyModel = responModel;
                    [weakSelf quickReplyButton];
                }
            } else {
                
            }
        } FailedBlock:^(NSError * _Nonnull error) {
            
        }];
    });
}
- (BaseAdjustButton *)quickReplyButton
{
    if (!_quickReplyButton) {
        BaseAdjustButton *button = [BaseAdjustButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        button.type = layoutTypeHorizontalLeftImageRightTitle;
        button.imageWidth = 10*ADAPTER_WIDTH;
        button.imageHeight = 6*ADAPTER_WIDTH;
        button.title = @"快问";
        button.fontWeight = UIFontWeightMedium;
        button.fontSize = 13*ADAPTER_WIDTH;
        button.marginLeft = 10*ADAPTER_WIDTH;
        button.marginItemX = 4*ADAPTER_WIDTH;
        button.marginRight = 10*ADAPTER_WIDTH;
        button.marginTop = 10*ADAPTER_WIDTH;
        button.marginBottom = 10*ADAPTER_WIDTH;
        [button begin];
        button.right = WIDTH - 16*ADAPTER_WIDTH;
        button.bottom = HEIGHT - 150*ADAPTER_WIDTH - kBottomSafeHeight;
        button.backgroundColor = rgba(0, 0, 0, 0.8);
        button.backgroundColor = rgba(76, 198, 253, 1);
        [button setTitleColor:WhiteColor forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"QuickReply_up"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"QuickReply_down"] forState:UIControlStateSelected];
        [button setTitle:@"快问" forState:UIControlStateNormal];
        [button setTitle:@"收起" forState:UIControlStateSelected];
        [button addTarget:self action:@selector(clickQuickReplyButton:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = button.height * 0.5;
        button.layer.masksToBounds = YES;
        _quickReplyButton = button;
    }
    return _quickReplyButton;
}

- (void)clickQuickReplyButton:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        sender.backgroundColor = rgba(0, 0, 0, 0.8);
        [sender setImage:[UIImage imageNamed:@"QuickReply_dwon"] forState:UIControlStateNormal];
        
        StoreQuickReplyView *view = [[StoreQuickReplyView alloc]initWithFrame:CGRectZero withParent:self withReplyButton:sender];
        view.quickReplyModel = self.quickReplyModel;
        WK(weakSelf)
        [view setCallBackContentBlock:^(NSString * _Nonnull content) {
            [weakSelf gotoChatWithContent:content];
        }];
        self.weakQuickReplyView = view;
    } else {
        sender.backgroundColor = rgba(76, 198, 253, 1);
        [sender setImage:[UIImage imageNamed:@"QuickReply_up"] forState:UIControlStateNormal];
        if (_weakQuickReplyView) {
            [self.weakQuickReplyView dismiss];
        }
    }
}
- (void)gotoChatWithContent:(NSString *)content {
    [[TT_IMContactManager manager] ImContactWithStoreId:self.packageDetailmodel.sellerModel.sellerId withQuickReply:content];
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (_quickReplyButton) {
        WK(weakSelf)
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.quickReplyButton.right = WIDTH - 16*ADAPTER_WIDTH;
        }];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (_quickReplyButton) {
        WK(weakSelf)
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.quickReplyButton.right = WIDTH - 16*ADAPTER_WIDTH;
        }];
    }
}
@end
